Ext.define('appmod.controller.framework.MainMenuPanel', {
    extend: 'Ext.app.Controller',


    refs: [
		{ref: 'mainMenuPanel',selector:'mainmenupanel'},
		{ref: 'mainCorePanel',selector:'maincorepanel'}
	],
	
    init: function(app) {
		// --- get app fomr application ---
		this.app = app;
    	
    	this.control({

			'mainmenupanel': {
			    execAction:{
					fn:this.execAction
				},
				opensubmenu:{
					fn:this.opensubmenu
				},
				beforerender: {
					fn:this.beforerender
				}
			}

    	});
    },
	
	beforerender:function(me , opts) {
		var toolbars = me.query("toolbar");
	},
	
	
	execAction:function(opts) {
		var menuPanel = this.getMainMenuPanel();

		var items = menuPanel.getDockedItems('toolbar[dock="bottom"]');
		var toolbar = items[0];		
		// --- remove docked items
		toolbar.removeAll(true);
		
		this.linkInPanel(opts['menu']);
	},
	
	linkInPanel:function(menu) {
		var url = menu.url;
		var corePanel = this.getMainCorePanel();
		var eOpts = {
			'eOpts':menu
		}
		this.clickSubmenu(null , null , eOpts);

	},
	
	
	checkResPermission:function(menu , definedAcl) {
		// --- fix  menu bug --
		if (!menu) {
			return false;
		}
		
		var menuPanel = this.getMainMenuPanel();
		// --- get auth data ---
		var authData = menuPanel.getAuthMenuData();
		
		var perm = null;
		for (var i = 0 ; i < authData.length ;i++) {
			perm = authData[i];
		
			if (menu.id == perm.id) {
				if ( definedAcl == 'deny') {
					return false;
				}
				else if ( definedAcl == 'access' ) {
					if (perm.acl == 'access') {
						return true;
					}
					else if (perm.acl == 'deny') {
						return false;
					}
					else {
						return false;
					}
				} 
			}
		}
		// --- default return true ---
		return true;
						
	},
	
	opensubmenu:function(opts) {
		me = this;
		var menuPanel = this.getMainMenuPanel();

		var items = menuPanel.getDockedItems('toolbar[dock="bottom"]');
		var toolbar = items[0];
		toolbar.removeAll(true);
		
		var orgMenu = opts['menu'];
		if (!orgMenu.leaf ) {
			
			var children = orgMenu['children'];
			// --- short menu ---
			children.sort(function(aMenu , bMenu) {
				if (aMenu.sortNum > bMenu.sortNum) {
				return 1;
				} else if (aMenu.sortNum < bMenu.sortNum){
					return -1;
				}
				return 0;
			});
						
			var submenu = null;
			for (var i = 0 ; i < children.length ; i++) {
				submenu = children[i];
				
				var permission = me.checkResPermission(submenu , 'access');

				if ( permission ) {

					// ---add last --
					var act = {
						text:submenu['text']
					};
					if (submenu.leaf) {
						act.listeners = {
							click: {
								fn:me.clickSubmenu,
								scope:me,
								eOpts :submenu
							}
						}

					} else {
						var subChildren = submenu.children;
						
						var submenus = Ext.create('Ext.menu.Menu');
						for (var j = 0 ; j < subChildren.length ;j++) {
							var endmenu = subChildren[j];
							
							var subPerm = me.checkResPermission(endmenu , 'access');
							
							if (subPerm) {
								submenus.add({
									text:endmenu['text'],
									listeners : {
										click: {
											fn:me.clickSubmenu,
											scope:me,
											eOpts :endmenu
										}										
									}
								});								
							}

						}
						act.menu = submenus;
						
					}
					toolbar.add(act);					
				}

			}
	
		}
	},
	
	// click subm menu
	clickSubmenu:function(me, event, eOpts) {
		compme = this;
		var app = compme.app;
		var mainCorePanel = compme.getMainCorePanel();

		mainCorePanel.removeAll();
		// --- add controller --
		var menu = eOpts['eOpts'];
		var action = menu.action;
		
		// --- parse protocol from action --
		var actObj = null;
		if (action.indexOf("ctl://") != -1) {
			var pos = action.indexOf("ctl://");
			var value = action.replace("ctl://","");
			actObj = {
				type:'ctl',
				value:value,
				prototal:'ctl://'
			}
		}
		
		
		if (actObj) {
			
			// --- handle for controller --
			if (actObj.type === 'ctl') {
				var ctlName = actObj.value;
				
				var contArray = null;
				if(ctlName.indexOf(',')>0){
					contArray = ctlName.split(',');
				}
				
				// --- not array ---
				if (contArray == null) {
					
					if (!app.getLaunchController(ctlName)) {
						app.addController(ctlName);
					}
						// --- open method 
					var controller = app.getController(ctlName);
					var container = compme.getMainCorePanel();
					
					if (controller.getDelayforOnLaunch() ) {
						var controllerPanel = controller.loadInitComp();
						container.add(controllerPanel);

						// --- start --
						controller.onLaunch(app);
						app.launchControllers[controller.id] = 1;				
					}					
				}
				
				// --- found array controllers 
				else {
					var cont = null;
					for(var i = 0 ;i<contArray.length ; i++){
						cont = contArray[i];
						if (!app.getLaunchController(cont)) {
							app.addController(cont);
						}
							// --- open method 
						var controller = app.getController(cont);
						var container = compme.getMainCorePanel();
						
						if (controller.getDelayforOnLaunch() ) {
							var controllerPanel = controller.loadInitComp();
							container.add(controllerPanel);
							
							// --- start --
							controller.onLaunch(app);
							app.launchControllers[controller.id] = 1;				
						}
					}					
				}

			}
			
		}

	},
	
	/**
	 * build menu for sub item
	 * @param {Object} orgMenu
	 */
	buildMenu:function(orgMenu) {
		me = this;
		
		var act = {
			text:orgMenu['text']
		};
		if (orgMenu.leaf) {
			act.handler = me.linkInPanel( orgMenu );
		
		} else {
			var subChildren = orgMenu.children;
			
			var submenus = Ext.create('Ext.menu.Menu');
			for (var j = 0 ; j < subChildren.length ;j++) {
				var endmenu = subChildren[j];
				var subAct = me.buildMenu(endmenu);		
				submenus.add(subAct);
			}
			act.menu = submenus;
					
		}		
		
		return act;		
	}
	
	
	
	
	
});